package icu.zhhll.util.lang;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

/**
 * 十六进制转换
 *
 * @author zh
 * @date 2023/8/9 10:43
 */
public class HexUtils {
    private final static char[] digits = "0123456789ABCDEF".toCharArray();

    /**
     * 16进制字符串转成byte数组
     *
     * @param hex 16进制字符串，支持大小写
     * @return byte数组
     */
    public static byte[] hexStringToBytes(String hex) {
        try {
            return Hex.decodeHex(hex);
        } catch (DecoderException e) {
            throw new RuntimeException("编码失败");
        }
    }

    /**
     * byte数组转成16进制字符串
     *
     * @param bytes byte数组
     * @return 16进制字符串
     */
    public static String bytesToHexString(byte[] bytes) {
        return new String(Hex.encodeHex(bytes, false));
    }
}
